import json

from django.shortcuts import render,HttpResponse,redirect
from app_user import models
from ..models import Goods,goods_slide_show,goods_type_show, Order, Shopping_Cart
from django.views.decorators.csrf import csrf_exempt
from datetime import datetime
import random
from app_user.utls.pagenation import Pagenation


def order(request):
    """"""
    """我的订单"""
    id = request.session.get('info')['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()

    order_data = Order.objects.filter(purchaser_id=id)
    order_num = order_data.count()
    order_obj = Pagenation(request=request, number_data=order_data)
    order_data = order_obj.page_data
    page_string = order_obj.page_string
    order_datas =[]
    if order_data != None:
        for item in order_data:
            goods_data = Goods.objects.filter(id=item.good_id).first()
            order_datas.append((item, goods_data))

    data = {
        "order_data": order_datas,
        "order_num": order_num,
        "cart_num": cart_num,
        "page_string":page_string,
    }

    return render(request, "order.html", context=data)





def add_order(request):
    """"""
    """购物车批量创建订单"""
    id = request.session.get('info')['id']
    cart_data =  models.Shopping_Cart.objects.filter(user_id=id).all()
    address_data = models.Address.objects.filter(user_id=id, is_default=True).first()
    cart_num = cart_data.count()
    goods_data = []
    totals = 0
    for item in cart_data:
        total = item.total
        totals += total
        num = item.goods_num
        goods__obj = Goods.objects.filter(id=item.goods_id).first()
        goods_data.append((goods__obj, num, total))

    if request.method == "GET":
        data = {
            "cart_num": cart_num,
            "goods_data": goods_data,
            "totals": totals,
            "address_data": address_data,
        }

        return render(request, "add_order.html" , context=data)

    for obj in goods_data:
        oid = datetime.now().strftime("%Y%m%d%H%M%S") + str(random.randint(1000, 9999))
        today = datetime.now()
        create_time = today.strftime("%Y-%m-%d")
        models.Order.objects.create(oid=oid, number=obj[1], price=obj[2], creat_time=create_time, order_status=1, consignee_info_id=address_data.id, good_id=obj[0].id, purchaser_id=id)
    models.Shopping_Cart.objects.filter(user_id=id).delete()

    return redirect('/order/')





def add_single_order(request,id, num):
    """"""
    """创建单个订单"""
    goods_id = id
    id = request.session.get('info')['id']
    cart_num = models.Shopping_Cart.objects.filter(user_id=id).all().count()
    address_data = models.Address.objects.filter(user_id=id, is_default=True).first()
    goods_data = models.Goods.objects.filter(id=goods_id).first()
    total = num * goods_data.price
    goods_num = num
    if request.method == "GET":
        data ={
            "cart_num":cart_num,
            "address_data": address_data,
            "goods_data": goods_data,
            "total": total,
            "goods_num": goods_num,
        }
        return render(request, "add_single_order.html", context=data)

    oid = datetime.now().strftime("%Y%m%d%H%M%S") + str(random.randint(1000, 9999))
    today = datetime.now()
    create_time = today.strftime("%Y-%m-%d")
    models.Order.objects.create(oid=oid, number=num,price=goods_data.price,creat_time=create_time,order_status=1, consignee_info_id=address_data.id, good_id=goods_id, purchaser_id=id)
    pay_id = Order.objects.filter(oid=oid).first().id
    return redirect(f'/alipay/{pay_id}/')


def delete_order(requests):
    """"""
    """取消订单"""
    oid = requests.GET.get("uid")
    Order.objects.filter(id=oid).delete()
    data = {
        "status": True,
    }
    return HttpResponse(json.dumps(data))







def create_order(request):
    """"""
    """生成订单"""